<template>
  <div class="top-right-btn">
    <el-row>
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button size="mini" circle icon="Refresh" @click="refresh()" />
      </el-tooltip>
      <el-dropdown class="" trigger="click" placement="bottom-end" v-if="columns.length">
        <el-tooltip class="item" effect="dark" content="显隐列" placement="top" >
          <el-button size="mini" circle icon="Menu" @click="showColumn()" />
        </el-tooltip>
        <template #dropdown>
          <el-dropdown-menu  >
            <el-row :gutter="20">
              <el-col :span="2">
                <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">列展示</el-checkbox>
              </el-col>
            </el-row>
            <hr/>
            <el-row :gutter="20" v-for="(item,index) in columnslist" :key="index">
              <el-col :span="2">
                <el-checkbox v-model="item.visible" @change="dataChange">{{item.label}}</el-checkbox>
              </el-col>
            </el-row>
          </el-dropdown-menu>
        </template>

      </el-dropdown>
    </el-row>

  </div>
</template>

<script setup>
const props = defineProps({
  columns: {
    type: Array,
    default: [],
  },
})
const emits = defineEmits(['queryTable']);
// 显隐数据
const value = ref([]);
// 弹出层标题
const title = ref("显示/隐藏");
// 是否显示弹出层
const open = ref(false);

const isIndeterminate = ref(false);
const columnslist = ref(props.columns);
const checkAll = ref(true);
// 刷新
function refresh() {
  emits("queryTable");
}

// 打开显隐列dialog
function showColumn() {
  open.value = true;
}

// 显隐列初始默认隐藏列
for (let item in props.columns) {
  if (props.columns[item].visible === false) {
    value.value.push(parseInt(item));
  }
}
// 右侧列表元素变化
function dataChange(data) {
  let count = 0;
  for (let item of columnslist.value){
    if (item.visible){
      count++
    }
  }
  isIndeterminate.value = count > 0 && count < columnslist.value.length;
  if (count == 0){
    checkAll.value = false;
  }
  else if(count == this.columnslist.length){
    checkAll.value = true;
  }
  /*for (var item in this.columns) {
    const key = this.columns[item].key;
    this.columns[item].visible = !data.includes(key);
  }*/
}
//全选
function handleCheckAllChange(e){
  isIndeterminate.value = false;
  for (let item of columnslist.value){
    item.visible = e;
  }
}
</script>

<style lang='scss' scoped>
  ::v-deep .el-transfer__button {
    border-radius: 50%;
    padding: 12px;
    display: block;
    margin-left: 0px;
  }
  ::v-deep .el-transfer__button:first-child {
    margin-bottom: 10px;
  }
  .el-dropdown{
    margin-left: 15px;
  }
  .el-dropdown-menu{
    width: 120px;
    padding: 10px 20px;
  }
  .el-checkbox--medium{
    height: 26px;
  }
</style>